home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 39.3 KB | 1,347 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // USection.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __USECTION__
- #include "USection.h"
- #endif
-
- // MacApp
-
- #ifndef __UADORNERS__
- #include "UAdorners.h"
- #endif
-
- #ifndef __UAPPLICATION__
- // #include "UApplication.h"
- #endif
-
- #ifndef __UCOMMAND__
- #include "UCommand.h"
- #endif
-
- #ifndef __UDEPENDENCIES__
- #include "UDependencies.h"
- #endif
-
- #ifndef __UDESIGNATOR__
- #include "UDesignator.h"
- #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- #ifndef __UDOCUMENT__
- #include "UDocument.h"
- #endif
-
- #ifndef __UEDITIONDOCUMENT__
- #include "UEditionDocument.h"
- #endif
-
- #ifndef __UEVENTHANDLER__
- #include "UEventHandler.h"
- #endif
-
- #ifndef __UEVENT__
- #include "UEvent.h"
- #endif
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- #ifndef __UFILEBASEDDOCUMENT__
- #include "UFileBasedDocument.h"
- #endif
-
- #ifndef __UFILE__
- #include "UFile.h"
- #endif
-
- #ifndef __ULIST__
- #include "UList.h"
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UPATCH__
- #include "UPatch.h"
- #endif
-
- #ifndef __USTREAM__
- #include "UStream.h"
- #endif
-
- #ifndef __UVIEW__
- #include "UView.h"
- #endif
-
- // Toolbox
-
- #ifndef __ALIASES__
- #include <Aliases.h>
- #endif
-
- #ifndef __APPLEEVENTS__
- #include <AppleEvents.h>
- #endif
-
- #ifndef __BALLOONS__
- #include <Balloons.h>
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __TEXTEDIT__
- #include <TextEdit.h>
- #endif
-
-
-
- //========================================================================================
- // CLASS TSection
- //========================================================================================
- #undef Inherited
- #define Inherited TObject
-
- #pragma segment MASectionNonRes
- MA_DEFINE_CLASS_M1(TSection, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TSection::TSection:
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- TSection::TSection()
- {
- fDocument = NULL;
- fDesignator = NULL;
- fSectionHandle = NULL;
- fRsrcID = 0;
- fLastEditionInfo.crDate = 0;
- fLastEditionInfo.mdDate = 0;
- fLastEditionInfo.fdCreator = '\?\?\?\?';
- fLastEditionInfo.fdType = '\?\?\?\?';
- fCanceled = FALSE;
- fFound = FALSE;
- fNewSection = TRUE;
- } // TSection::TSection
-
- //----------------------------------------------------------------------------------------
- // TSection::ISection:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSection::ISection(TEditionDocument* itsDocument,
- TDesignator* itsDesignator,
- SectionHandle itsSectionHandle,
- short itsRsrcID)
-
- {
- this->IObject();
-
- fDocument = itsDocument;
- fDesignator = itsDesignator;
- fSectionHandle = itsSectionHandle;
- fRsrcID = itsRsrcID;
- fFound = kEditionFound;
- fCanceled = kDontCancel;
- fNewSection = TRUE; // yes it is new
- } // TSection::ISection
-
- //----------------------------------------------------------------------------------------
- // TSection::Associate:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TSection::Associate(TFile* aFile)
- {
- #if qDebug
- if (!fSectionHandle)
- ProgramBreak("###fSectionHandle is NULL");
- #endif
-
- // get the FSSpec for the file in question
- FSSpec itsFSSpec;
- aFile->GetFileSpec(itsFSSpec);
-
- // associate this section object with the FSSpec
- OSErr err = AssociateSection(fSectionHandle, &itsFSSpec);
-
- // Allow fnfErr if we're a subscriber. This accounts for the situation in which the
- // user has canceled the publisher so that its edition container file is no longer
- // there. Should this be done in an override, e.g. TSubscriber::Associate() ???
- if (this->GetSectionType() == stSubscriber)
- {
- if ((err != noErr) && (err != fnfErr))
- FailOSErr(err);
- }
- else
- FailOSErr(err);
-
- // assume it's changed
- this->MarkAsChanged();
- } // TSection::Associate
-
- //----------------------------------------------------------------------------------------
- // TSection::AttachSectionHandle:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSection::AttachSectionHandle(SectionHandle itsSectionHandle)
- {
- #if qDebug
- if (fSectionHandle)
- ProgramBreak("###about to overwrite fSectionHandle");
- #endif
-
- fSectionHandle = itsSectionHandle;
- } // TSection::AttachSectionHandle
-
- //----------------------------------------------------------------------------------------
- // TSection::DoNeedDiskSpace:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TSection::DoNeedDiskSpace(long& ,
- long& rsrcForkBytes)
- {
- if (fSectionHandle)
- {
- EditionInfoRecord editionInfo;
-
- FailOSErr(GetEditionInfo(fSectionHandle, &editionInfo));
- CStr255 rsrcName(editionInfo.container.theFile.name);
- rsrcForkBytes += (3 * (rsrcName.Length() + kLengthByte));
-
- rsrcForkBytes += kRsrcOverhead + GetHandleSize((Handle)fSectionHandle);
- rsrcForkBytes += kRsrcOverhead + GetHandleSize((Handle)(*fSectionHandle)->alias);
-
- TCountingStream* aStream;
- aStream = new TCountingStream;
- aStream->ICountingStream();
- aStream->WriteStreamObject(fDesignator, kStandardObject);
- rsrcForkBytes += kRsrcOverhead + aStream->GetSize();
- aStream->Free();
- }
- } // TSection::DoNeedDiskSpace
-
- //----------------------------------------------------------------------------------------
- // TSection::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionClose
-
- TSection::~TSection()
- {
- fDocument->RemoveSection(this);
-
- this->UnRegister();
- fDesignator = (TDesignator *)FreeIfObject(fDesignator);
- fSectionHandle = DisposeIfSectionHandle(fSectionHandle);
- } // TSection::Free
-
- //----------------------------------------------------------------------------------------
- // TSection::GetEditionName:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSection::GetEditionName(CStr255& name)
- {
- this->UpdateEditionInfo();
- name = fLastEditionInfo.container.theFile.name;
- } // TSection::GetEditionName
-
- //----------------------------------------------------------------------------------------
- // TSection::GetFileHandler:
- //----------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------
- // TSection::GetSectionType:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- SectionType TSection::GetSectionType()
- {
- if (fSectionHandle)
- return (*fSectionHandle)->kind;
- else
- return (SectionType) - 1;
- } // TSection::GetSectionType
-
- //----------------------------------------------------------------------------------------
- // TSection::GetUpdateMode:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- UpdateMode TSection::GetUpdateMode()
- {
- if (fSectionHandle)
- return (*fSectionHandle)->mode;
- else
- return (UpdateMode) - 1;
- } // TSection::GetUpdateMode
-
- //----------------------------------------------------------------------------------------
- // TSection::DoRead:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRead
-
- void TSection::DoRead(TFile* aFile)
- {
- if (!aFile->IsRsrcForkOpen())
- {
- #if qDebug
- ProgramBreak("###resource fork isn't open");
- #endif
- }
- else
- {
- // set up for failure handler
- MAVolatileInit(TResourceStream*, aResourceStream, NULL);
- MAVolatile(short, oldResFile);
-
- fSectionHandle = NULL;
- fDesignator = NULL;
-
- FailInfo fi;
- Try(fi)
- {
- Handle aHandle;
-
- oldResFile = aFile->UseResource(); // set the current ResFile
-
- // read in the section handle from a rSectionType resource
- aHandle = Get1Resource(rSectionType, fRsrcID);
- FailNILResource(aHandle);
- DetachResource(aHandle);
- FailResError();
- fSectionHandle = (SectionHandle)aHandle;
- (*fSectionHandle)->alias = NULL; // so failure handler works
-
- // read in the alias handle from a rAliasType resource
- aHandle = Get1Resource(rAliasType, fRsrcID);
- FailNILResource(aHandle);
- DetachResource(aHandle);
- FailResError();
- (*fSectionHandle)->alias = (AliasHandle)aHandle;
-
- // read in the associated designator
- aResourceStream = new TResourceStream;
- aResourceStream->IResourceStream(kDesignatorResType, fRsrcID);
-
- FailInfo innerfi;
- Try(innerfi)
- {
- TDesignator* aDesignator = NULL;
- if (aResourceStream->ReadStreamObject((TObject*&) aDesignator))
- fDesignator = aDesignator;
- #if qDebugMsg
- else
- ProgramBreak("###Unknown TDesignator in TSection::DoRead.");
- #endif
- innerfi.Success();
- }
- else
- {
- aResourceStream = (TResourceStream *)FreeIfObject(aResourceStream);
- fSectionHandle = DisposeIfSectionHandle(fSectionHandle);
- // We don't need to free aDesignator here because it would have previously been freed
- innerfi.ReSignal();
- }
-
- aResourceStream->Free();
-
- MAUseResFile(oldResFile); // restore the current ResFile
-
- fi.Success();
- }
- else // Recover
- {
- fSectionHandle = DisposeIfSectionHandle(fSectionHandle);
-
- MAUseResFile(oldResFile); // restore the current ResFile
-
- fi.ReSignal(); // Optional. Omit to continue.
- }
-
- // register the section
- fNewSection = FALSE; // no it is not new
- OSErr err = this->Register();
- fFound = (err == containerNotFoundWrn) ? kEditionNotFound : kEditionFound;
- if ((err != containerNotFoundWrn) && (err != notThePublisherWrn) && (err != multiplePublisherWrn))
- FailOSErr(err);
-
- // associate the section correctly
- this->Associate(aFile);
- }
- } // TSection::DoRead
-
- //----------------------------------------------------------------------------------------
- // TSection::DoWrite:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TSection::DoWrite(TFile* aFile,
- Boolean /* makingCopy */)
- {
- if (!fSectionHandle)
- {
- #if qDebug
- ProgramBreak("###TSection::DoWrite: fSectionHandle is NULL");
- #endif
- return;
- }
-
- if (!aFile->IsRsrcForkOpen())
- {
- #if qDebug
- ProgramBreak("###TSection::DoWrite: resource fork isn't open");
- #endif
- return;
- }
-
- MAVolatile(short, oldResFile);
-
- FailInfo fi;
- Try(fi)
- {
- //MAVolatile(Handle, aHandle);
- Handle aHandle; // can't be volatile because it is passed by reference
-
- oldResFile = aFile->UseResource(); // set the current ResFile
-
- // associate with the correct current file
- TFile* currentFile = this->GetCurrentFile(aFile);
- this->Associate(currentFile);
-
- // prepare to write out our section resources
- EditionInfoRecord editionInfo;
- FailOSErr(GetEditionInfo(fSectionHandle, &editionInfo));
- CStr255 rsrcName = editionInfo.container.theFile.name;
-
- // add the alias handle as an rAliasType resource
- aHandle = (Handle)(*fSectionHandle)->alias;
- PermHandToHand(aHandle);
- FailInfo innerfi1;
- Try(innerfi1)
- {
- MAAddResource(aHandle, rAliasType, fRsrcID, rsrcName);
- FailResError();
- innerfi1.Success();
- }
- else
- {
- aHandle = DisposeIfHandle(aHandle);
- innerfi1.ReSignal();
- }
-
- // add the section handle as an rSectionType resource
- aHandle = (Handle)fSectionHandle;
- PermHandToHand(aHandle);
- FailInfo innerfi2;
- Try(innerfi2)
- {
- MAAddResource(aHandle, rSectionType, fRsrcID, rsrcName);
- FailResError();
- innerfi2.Success();
- }
- else
- {
- aHandle = DisposeIfHandle(aHandle);
- innerfi2.ReSignal();
- }
-
- // ask the designator to write its specification to a TResourceStream
- aHandle = NewPermHandle(2);
- FailInfo innerfi3;
- Try(innerfi3)
- {
- MAAddResource(aHandle, kDesignatorResType, fRsrcID, rsrcName);
- FailResError();
- innerfi3.Success();
- }
- else
- {
- aHandle = DisposeIfHandle(aHandle);
- innerfi3.ReSignal();
- }
- WriteResource(aHandle);
- ReleaseResource(aHandle);
-
- // write the designator to a resource (using a stream)
- MAVolatileInit(TResourceStream*, aResourceStream, new TResourceStream);
- aResourceStream->IResourceStream(kDesignatorResType, fRsrcID);
-
- FailInfo innerfi4;
- Try(innerfi4)
- {
- aResourceStream->WriteStreamObject(fDesignator, kStandardObject);
- innerfi4.Success();
- }
- else
- {
- aResourceStream = (TResourceStream *)FreeIfObject(aResourceStream);
- innerfi4.ReSignal();
- }
-
- aResourceStream = (TResourceStream *)FreeIfObject(aResourceStream);
-
- MAUseResFile(oldResFile); // restore the current ResFile
-
- fNewSection = FALSE; // no it is not new
- fi.Success();
- }
- else // Recover
- {
- MAUseResFile(oldResFile); // restore the current ResFile
- fi.ReSignal(); // Optional. Omit to continue.
- }
- } // TSection::DoWrite
-
- //----------------------------------------------------------------------------------------
- // TSection::Delete:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TSection::Delete()
- {
- } // TSection::Delete
-
- //----------------------------------------------------------------------------------------
- // TSection::IsCanceled:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- Boolean TSection::IsCanceled()
- {
- return fCanceled;
- } // TSection::IsCanceled
-
- //----------------------------------------------------------------------------------------
- // TSection::IsChanged: this method is never called: it is overridden for both TPublisher
- // & TSubscriber
- //----------------------------------------------------------------------------------------
- #pragma segment MANever
-
- Boolean TSection::IsChanged()
- {
- return FALSE;
- } // TSection::IsChanged
-
- //----------------------------------------------------------------------------------------
- // TSection::MarkAsChanged: this method is never called: it is overridden for both
- // TPublisher & TSubscriber
- //----------------------------------------------------------------------------------------
- #pragma segment MANever
-
- void TSection::MarkAsChanged()
- {
- } // TSection::MarkAsChanged
-
- //----------------------------------------------------------------------------------------
- // TSection::IsRegistered:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- Boolean TSection::IsRegistered()
- {
- // it's registered if the Edition Manager thinks it's registered
- if (fSectionHandle)
- return IsRegisteredSection(fSectionHandle) == noErr;
- else
- return FALSE;
- } // TSection::IsRegistered
-
- //----------------------------------------------------------------------------------------
- // TSection::Register:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- OSErr TSection::Register()
- {
- OSErr err = noErr;
-
- if (!this->IsRegistered())
- {
- TFile* aFile = (fDocument != NULL) ? fDocument->GetFile() : NULL;
- if (aFile)
- {
- FSSpec theFileSpec;
-
- Boolean aliasWasUpdated;
- aFile->GetFileSpec(theFileSpec);
- err = RegisterSection(&theFileSpec, fSectionHandle, &aliasWasUpdated);
-
- // if aliasWasUpdated, mark section as changed so that user saves changed edition
- if (aliasWasUpdated)
- this->MarkAsChanged();
-
- // if RegisterSection was successful, update edition info
- if (err == noErr)
- this->UpdateEditionInfo();
- }
- else
- {
- err = fnfErr;
- #if qDebug
- ProgramBreak("how to call RegisterSection if there isn't a TFile???");
- #endif
- }
- }
- return err;
- } // TSection::Register
-
- //----------------------------------------------------------------------------------------
- // TSection::UnRegister:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSection::UnRegister()
- {
- if (this->IsRegistered())
- FailOSErr(UnRegisterSection(fSectionHandle));
- } // TSection::UnRegister
-
- //----------------------------------------------------------------------------------------
- // TSection::UpdateEditionInfo:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSection::UpdateEditionInfo()
- {
- EditionInfoRecord editionInfo;
-
- if (this->IsRegistered() && (GetEditionInfo(fSectionHandle, &editionInfo) == noErr))
- fLastEditionInfo = editionInfo;
- } // TSection::UpdateEditionInfo
-
- //----------------------------------------------------------------------------------------
- // TSection::CancelSection:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSection::CancelSection(Boolean cancel)
- {
- OSErr err;
-
- if (cancel != fCanceled)
- if (cancel == kCancel)
- {
- this->UpdateEditionInfo(); // We will be able to delete it later.
- this->UnRegister();
- fCanceled = kCancel; // it IS canceled
- }
- else
- {
- err = this->Register();
- if ((err != containerNotFoundWrn) && (err != notThePublisherWrn) && (err != multiplePublisherWrn))
- FailOSErr(err);
- fCanceled = kDontCancel; // it IS !canceled
- }
- } // TSection::CancelSection
-
- //----------------------------------------------------------------------------------------
- // TSection::GetCurrentFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- TFile* TSection::GetCurrentFile(TFile* aFile)
- {
- // This looks strange but it should be correct. The idea is to associate the
- // section with the correct disk file. If it's a newly saved document, associate
- // with the file that is passed in, this will be the temporary file; if it's not a
- // new save, associate it with the current file that the file handler owns.
-
- if (fDocument && fDocument->FileExists())
- return fDocument->GetFile();
- else
- return aFile;
- } // TSection::GetCurrentFile
-
-
- //========================================================================================
- // CLASS TPublisher
- //========================================================================================
- #undef Inherited
- #define Inherited TSection
-
- #pragma segment MASectionNonRes
- MA_DEFINE_CLASS_M1(TPublisher, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TPublisher::TPublisher: Empty constructor to satisfy the compiler.
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- TPublisher::TPublisher()
- {
- } // TPublisher::TPublisher
-
- //----------------------------------------------------------------------------------------
- // TPublisher destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TPublisher::~TPublisher()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TPublisher::IPublisher:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionNonRes
-
- void TPublisher::IPublisher(TEditionDocument* itsDocument,
- TDesignator* itsDesignator,
- SectionHandle itsSectionHandle,
- short itsRsrcID)
- {
- this->ISection(itsDocument, itsDesignator, itsSectionHandle, itsRsrcID);
- } // TPublisher::IPublisher
-
- //----------------------------------------------------------------------------------------
- // TPublisher::DoUpdate:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TPublisher::DoUpdate(ChangeID theChange,
- TObject* changedObject,
- TObject* changedBy,
- TDependencySpace* dependencySpace)// override
- {
- if (theChange != kNoChange)
- this->MarkAsChanged();
-
- Inherited::DoUpdate(theChange, changedObject, changedBy,dependencySpace);
- } // TPublisher::DoUpdate
-
- //----------------------------------------------------------------------------------------
- // TPublisher::Publish:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TPublisher::Publish(TFile* aFile)
- {
- FSSpec aFSSpec;
- FSSpecPtr aFSSpecPtr = NULL;
- OSErr err;
-
- MAVolatile(TSectionStream*, aSectionStream);
- //MAVolatile(EditionRefNum, refNum);
- EditionRefNum refNum; // can't be volatile because it is passed by reference
-
- if (aFile && aFile->HasValidFileSpec())
- {
- aFile->GetFileSpec(aFSSpec);
- aFSSpecPtr = &aFSSpec;
- }
-
- err = OpenNewEdition(fSectionHandle, gDispatcher->fCreator, aFSSpecPtr, &refNum);
- if (err == noErr) // DON'T signal failure if the OpenNewEdition
- // fails, just continue without publishing.
- {
- FailInfo fi;
- Try(fi)
- {
- // Create a section stream to write the published data to, and publish the
- // document's private type first:
- aSectionStream = NULL;
- aSectionStream = new TSectionStream;
- aSectionStream->ISectionStream(refNum, fDocument->fScrapType);
-
- FailInfo innerfi;
- Try(innerfi)
- {
- fDocument->DoWriteData(fDocument->fScrapType, fDesignator, aSectionStream);
-
- // Publish the rest of the data types. The order in which the application
- // publishes to the edition container file indicates to the client application
- // the preferred order for subscribers! So, publish the TEXT or PICT or 'snd '
- // (at least one is required) in the order prefered.
-
- // N.B.: The Edition Manager will set the edition container file's file type
- // according to which type was published first: 'edtp' if PICT was published
- // first, 'edtt' if TEXT was published first, and 'edts' if 'snd ' was
- // published first.
-
- if (fDocument->GetPublishPreference() == 'PICT')
- {
- aSectionStream->Reset('PICT', 0);
- fDocument->DoWriteData('PICT', fDesignator, aSectionStream);
- aSectionStream->Reset('TEXT', 0);
- fDocument->DoWriteData('TEXT', fDesignator, aSectionStream);
- aSectionStream->Reset('snd ', 0);
- fDocument->DoWriteData('snd ', fDesignator, aSectionStream);
- }
- else if (fDocument->GetPublishPreference() == 'TEXT')
- {
- aSectionStream->Reset('TEXT', 0);
- fDocument->DoWriteData('TEXT', fDesignator, aSectionStream);
- aSectionStream->Reset('PICT', 0);
- fDocument->DoWriteData('PICT', fDesignator, aSectionStream);
- aSectionStream->Reset('snd ', 0);
- fDocument->DoWriteData('snd ', fDesignator, aSectionStream);
- }
- else if (fDocument->GetPublishPreference() == 'snd ')
- {
- aSectionStream->Reset('snd ', 0);
- fDocument->DoWriteData('snd ', fDesignator, aSectionStream);
- aSectionStream->Reset('TEXT', 0);
- fDocument->DoWriteData('TEXT', fDesignator, aSectionStream);
- aSectionStream->Reset('PICT', 0);
- fDocument->DoWriteData('PICT', fDesignator, aSectionStream);
- }
- innerfi.Success();
- }
- else
- {
- // Free the stream
- aSectionStream = (TSectionStream*)FreeIfObject(aSectionStream);
- innerfi.ReSignal();
- }
-
- // Free the stream
- aSectionStream->Free();
-
- fi.Success();
- }
- else // Recover
- {
- // If there is a failure during publishing close the edition file without
- // saving the changes (pass false for successful). This ensures that the
- // publisher will get a shot at publishing again later.
- err = CloseEdition(refNum, FALSE);
-
- fi.ReSignal(); // Optional. Omit to continue.
- }
- // close the edition file, passing true for the successful parameter
- FailOSErr(CloseEdition(refNum, TRUE));
- }
- } // TPublisher::Publish
-
- //----------------------------------------------------------------------------------------
- // TPublisher::DoWrite:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TPublisher::DoWrite(TFile* aFile,
- Boolean makingCopy)// override
-
- {
- Inherited::DoWrite(aFile, makingCopy);
-
- if ((!fDocument->fStopAllEditions) && fSectionHandle && (this->GetUpdateMode() == pumOnSave) && this->IsChanged())
- {
- TFile* currentFile = this->GetCurrentFile(aFile);
- if (currentFile)
- this->Publish(currentFile);
- else
- this->Publish(aFile);
- }
- } // TPublisher::DoWrite
-
- //----------------------------------------------------------------------------------------
- // TPublisher::Delete:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TPublisher::Delete() // override
-
- {
- this->DeleteEditionFile();
-
- Inherited::Delete();
- } // TPublisher::Delete
-
- //----------------------------------------------------------------------------------------
- // TPublisher::DeleteEditionFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TPublisher::DeleteEditionFile()
-
- {
- FSSpec theFile;
-
- // get the edition container file's FSSpec - from updated fLastEditionInfo
- this->UpdateEditionInfo();
- theFile = fLastEditionInfo.container.theFile;
-
- // cancel the section - this unregisters the section
- this->CancelSection(kCancel);
-
- // delete the edition container file
- OSErr err = DeleteEditionContainerFile(&theFile);
- if ((err != noErr) && (err != fnfErr)) // allow file-not-found error
- FailOSErr(err);
- } // TPublisher::DeleteEditionFile
-
- //----------------------------------------------------------------------------------------
- // TPublisher::IsChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- Boolean TPublisher::IsChanged()
-
- {
- EditionInfoRecord editionInfo;
-
- if (fSectionHandle)
- {
- FailOSErr(GetEditionInfo(fSectionHandle, &editionInfo));
- return editionInfo.mdDate != (*fSectionHandle)->mdDate;
- }
- else
- return FALSE;
- } // TPublisher::IsChanged
-
- //----------------------------------------------------------------------------------------
- // TPublisher::MarkAsChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TPublisher::MarkAsChanged()
-
- {
- if (fSectionHandle)
- {
- unsigned long secs;
-
- // mark this publisher as changed
- GetDateTime(&secs);
- (*fSectionHandle)->mdDate = secs;
- }
- } // TPublisher::MarkAsChanged
-
-
-
- //========================================================================================
- // CLASS TSubscriber
- //========================================================================================
- #undef Inherited
- #define Inherited TSection
-
- #pragma segment MASectionNonRes
- MA_DEFINE_CLASS_M1(TSubscriber, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::TSubscriber: Empty constructor to satisfy the compiler.
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- TSubscriber::TSubscriber()
- {
- } // TSubscriber::TSubscriber
-
- //----------------------------------------------------------------------------------------
- // TSubscriber destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TSubscriber::~TSubscriber()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::ISubscriber:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSubscriber::ISubscriber(TEditionDocument* itsDocument,
- TDesignator* itsDesignator,
- SectionHandle itsSectionHandle,
- short itsRsrcID)
-
- {
- this->ISection(itsDocument, itsDesignator, itsSectionHandle, itsRsrcID);
-
- fChanged = FALSE; // should be in an Initialize method.
-
- FailInfo fi;
- Try(fi)
- {
- // ensure that the document is notified of any and all changes to the subscriber
- this->AddDependent(itsDocument);
-
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- } // TSubscriber::ISubscriber
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::SubscribeIfNewer:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRead
-
- void TSubscriber::SubscribeIfNewer()
-
- {
- if ((!fDocument->fStopAllEditions) && fSectionHandle && (this->GetUpdateMode() == sumAutomatic) && this->IsNewerEditionAvailable())
- this->Subscribe();
- } // TSubscriber::SubscribeIfNewer
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::Subscribe:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRead
-
- void TSubscriber::Subscribe()
-
- {
- Size formatSize;
- OSErr err;
-
- MAVolatile(TSectionStream*, aSectionStream);
- //MAVolatile(EditionRefNum, refNum);
- EditionRefNum refNum; // can't be volatile because it is passed by reference
-
- FailOSErr(OpenEdition(fSectionHandle, &refNum));
-
- FailInfo fi;
- Try(fi)
- {
- // Create a section stream to read the published data from.
- aSectionStream = NULL;
- aSectionStream = new TSectionStream;
- aSectionStream->ISectionStream(refNum, ' ');
-
- FailInfo innerfi;
- Try(innerfi)
- {
- // Top priority: subscribe to private types.
- err = EditionHasFormat(refNum, fDocument->fScrapType, &formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset(fDocument->fScrapType, formatSize);
- fDocument->DoReadData(fDocument->fScrapType, fDesignator, aSectionStream, formatSize);
- }
- else
- {
- // subscribe to one or more of the required data types
- err = EditionHasFormat(refNum, 'PICT', &formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset('PICT', formatSize);
- fDocument->DoReadData('PICT', fDesignator, aSectionStream, formatSize);
- }
- err = EditionHasFormat(refNum, 'TEXT', &formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset('TEXT', formatSize);
- fDocument->DoReadData('TEXT', fDesignator, aSectionStream, formatSize);
- }
- err = EditionHasFormat(refNum, 'snd ', &formatSize);
- if ((err == noErr) && (formatSize > 0))
- {
- aSectionStream->Reset('snd ', formatSize);
- fDocument->DoReadData('snd ', fDesignator, aSectionStream, formatSize);
- }
- }
- innerfi.Success();
- }
- else
- {
- // Free the stream
- aSectionStream = (TSectionStream*)FreeIfObject(aSectionStream);
- innerfi.ReSignal();
- }
-
- // Free the stream
- aSectionStream->Free();
-
- fi.Success();
- }
- else // Recover
- {
- // If there is a failure during subscribing, close the edition file without saving
- // the changes (pass false for successful). This ensures that the subscriber will
- // get a shot at subscribing again later.
- err = CloseEdition(refNum, FALSE);
-
- fi.ReSignal(); // Optional. Omit to continue.
- }
-
- // close the edition file
- FailOSErr(CloseEdition(refNum, TRUE));
-
- // adjust this guy's border
- fDocument->DoAdjustBorder(this);
-
- this->Changed(cSubscriberChanged, this); // notify dependents
- } // TSubscriber::Subscribe
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::IsNewerEditionAvailable:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- Boolean TSubscriber::IsNewerEditionAvailable()
-
- {
- EditionInfoRecord editionInfo;
-
- if (fSectionHandle)
- {
- FailOSErr(GetEditionInfo(fSectionHandle, &editionInfo));
- return (editionInfo.mdDate != (*fSectionHandle)->mdDate);
- }
- else
- return FALSE;
- } // TSubscriber::IsNewerEditionAvailable
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::OpenPublisher:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSubscriber::OpenPublisher()
-
- {
- EditionInfoRecord editionInfo;
-
- FailOSErr(GetEditionInfo(fSectionHandle, &editionInfo));
- FailOSErr(GoToPublisherSection(&(editionInfo.container)));
- } // TSubscriber::OpenPublisher
-
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::IsChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- Boolean TSubscriber::IsChanged()
-
- {
- return fChanged;
- } // TSubscriber::IsChanged
-
- //----------------------------------------------------------------------------------------
- // TSubscriber::MarkAsChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSubscriber::MarkAsChanged()
-
- {
- fChanged = TRUE;
- } // TSubscriber::MarkAsChanged
-
-
- //========================================================================================
- // CLASS TSectionStream
- //========================================================================================
- #undef Inherited
- #define Inherited TStream
-
- #pragma segment MASectionNonRes
- MA_DEFINE_CLASS_M1(TSectionStream, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TSectionStream constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamCreate
-
- TSectionStream::TSectionStream()
- {
- fEditionRefNum = 0;
- fEditionOSType = kNoIdentifier;
- fPosition = 0;
- fSize = 0;
- } // TSectionStream::TSectionStream
-
- //----------------------------------------------------------------------------------------
- // TSectionStream destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TSectionStream::~TSectionStream()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::ISectionStream:
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamCreate
-
- void TSectionStream::ISectionStream(EditionRefNum itsEditionRefNum, OSType itsEditionOSType)
- {
- this->IStream();
-
- fEditionRefNum = itsEditionRefNum;
- fEditionOSType = itsEditionOSType;
- } // TSectionStream::ISectionStream
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::Reset:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionWrite
-
- void TSectionStream::Reset(OSType itsEditionOSType, long itsSize)
- {
- this->SetPosition(0);
- fEditionOSType = itsEditionOSType;
- fSize = itsSize;
- } // TSectionStream::Reset
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::GetPosition:
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- long TSectionStream::GetPosition() // override
- {
- return fPosition;
- } // TSectionStream::GetPosition
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::SetPosition:
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- void TSectionStream::SetPosition(long newPosition)// override
- {
- fPosition = newPosition;
-
- if (fPosition > fSize) // Adjust the size if necessary.
- fSize = fPosition;
- } // TSectionStream::SetPosition
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::GetSize:
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- long TSectionStream::GetSize()
- {
- return fSize;
- } // TSectionStream::GetSize
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::SetSize:
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamUtil
-
- void TSectionStream::SetSize(long newSize)// override
- {
- fSize = newSize;
-
- if (newSize < fPosition) // If we shrunk the size, then we have to adjust the position.
- fPosition = newSize;
- } // TSectionStream::SetSize
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::WriteBytes:
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamWrite
-
- void TSectionStream::WriteBytes(const void* p, long count)
- {
- FailOSErr(SetEditionFormatMark(fEditionRefNum, fEditionOSType, this->GetPosition()));
- FailOSErr(WriteEdition(fEditionRefNum, fEditionOSType, p, count));
-
- this->SetPosition(fPosition + count); // Advance the position.
- } // TSectionStream::WriteBytes
-
- //----------------------------------------------------------------------------------------
- // TSectionStream::ReadBytes:
- //----------------------------------------------------------------------------------------
- #pragma segment MAStreamRead
-
- void TSectionStream::ReadBytes(void* p, long count) // override
- {
- FailOSErr(SetEditionFormatMark(fEditionRefNum, fEditionOSType, this->GetPosition()));
- FailOSErr(ReadEdition(fEditionRefNum, fEditionOSType, p, &count));
-
- this->SetPosition(fPosition + count); // Advance the position.
- } // TSectionStream::ReadBytes
-
-
-
- //========================================================================================
- // CLASS TSectionList
- //========================================================================================
- #undef Inherited
- #define Inherited TSortedList
-
- #pragma segment MASectionNonRes
- MA_DEFINE_CLASS_M1(TSectionList, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TSectionList::TSectionList: Empty constructor to satisfy the compiler.
- //----------------------------------------------------------------------------------------
- #pragma segment ConstructorRes
-
- TSectionList::TSectionList()
- {
- } // TSectionList::TSectionList
-
- //----------------------------------------------------------------------------------------
- // TSectionList destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TSectionList::~TSectionList()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TSectionList::ISectionList:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- void TSectionList::ISectionList()
- {
- this->ISortedList();
- } // TEntriesList::ISectionList
-
- //----------------------------------------------------------------------------------------
- // TSectionList::Compare:
- //----------------------------------------------------------------------------------------
- #pragma segment MASectionRes
-
- CompareResult TSectionList::Compare(TObject* item1,
- TObject* item2)
- {
- TSection* section1 = (TSection*) item1;
- TSection* section2 = (TSection*) item2;
-
- if ((*(section1->fSectionHandle))->controlBlock < (*(section2->fSectionHandle))->controlBlock)
- return kItem1LessThanItem2;
- else if ((*(section1->fSectionHandle))->controlBlock > (*(section2->fSectionHandle))->controlBlock)
- return kItem1GreaterThanItem2;
- else
- return kItem1EqualItem2;
- } // TSectionList::Compare
-
- //----------------------------------------------------------------------------------------
- // End of USection.cp
-
- #pragma segment Inline
-